<template>
  <div>
    <el-dialog
      title="添加经纪"
      :visible="dialogVisible"
      width="60%"
      @close="closeFn"
    >
      <!-- <span>这是一段信息</span> -->
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="formData.email" style="width:80%" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="formData.phone" style="width:80%" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="formData.address" style="width:80%" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" style="width:80%" />
        </el-form-item>
        <el-form-item label="封面">
          <UploadFile ref="img" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addBusiness, editBusiness, correctBusiness } from '@/api/business'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
    // id: {
    //   type: Number,
    //   required: true
    // }
  },
  data() {
    return {
      imageUrl: '',
      formData: {
        image: '',
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空！', trigger: 'blur' },
          { min: 1, max: 50, message: '名称长度为1到50个字符', trigger: 'blur' }
          // 自定义校验规则 validator：饿了么UI提供的自定义校验规则的属性
        ],
        desc: [
          { required: true, message: '描述不能为空！', trigger: 'blur' },
          { min: 1, max: 1000000, message: '请输入描述', trigger: 'blur' }
          // 自定义校验规则 validator：饿了么UI提供的自定义校验规则的属性
        ]
      }
    }
  },
  created() {
    const res = editBusiness(this.id)
    console.log(77, res)
  },

  methods: {
    // 点击按钮的时候调用该函数
    async getEditById(id) {
      console.log(84, id)
      this.formData = await editBusiness(id)
      console.log(86, this.formData)
      // console.log(51, res)
      this.$refs.img.fileList.push({
        url: this.formData.image
      })
    },
    // 确定
    async confirm() {
      // 表单校验
      await this.$refs.form.validate()

      const obj = this.$refs.img.fileList[0]
      this.formData.image = obj.url
      if (this.formData.id) {
        // console.log(96, this.formData)
        // 编辑
        await correctBusiness(this.formData)
      } else {
        // 新增数据
        await addBusiness(this.formData)
      }
      // 关闭弹窗
      // this.closeFn()
      this.closeFn()
      // 清空表单
      this.$refs.form.resetFields()
      // 清空图片
      this.$refs.img.removeFn()
      // 刷新数据
      this.$emit('updateData')
    },
    // 关闭弹窗
    closeFn() {
      // 父子传参时，子组件接受到参数后不能直接修改，用一个回调函数来调用父组件进行修改
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
